<template>
  <div class="app">
    <div class="search-bar">
      <el-form :model="searchData" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="5">
            <el-form-item label="公司名称" style="margin-bottom: 5px;">
              <el-input v-model="searchData.companyName" size="medium" />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="公司ID" style="margin-bottom: 5px;">
              <el-input v-model="searchData.companyCode" size="medium" />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="同步频率类型" style="margin-bottom: 5px;">
              <el-select v-model="searchData.syncType" @change="handleSelect" size="medium" class="select">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="同步日期" style="margin-bottom: 5px;">
              <el-input v-model="searchData.syncFreq" size="medium" @input="inputChange" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <div style="margin-top: 3px;">
              <el-button icon="el-icon-search" type="primary" size="small" @click="handleSearch">查询</el-button>
              <el-button icon="el-icon-refresh-left" size="small" @click="handleClear">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div ref="ref2" class="table-box">
      <tableComponent
        class="table"
        :height="height"
        :data="tableData || []"
        :columns="rateManageColumns"
        @change="tablePage"
        :table-option="tableOption"
        :page-option="pageOption"
        :total="total"
      ></tableComponent>
    </div>
  </div>
</template>

<script>
import tableComponent from '@/components/Table/table'
import { rateManageColumns } from "./index.js"
import { getrateManage } from '@/api/modules/qzConnect.js'
export default {
  components: {
    tableComponent
  },
  data() {
    return {
      searchData: {
        companyName: '',
        companyCode: '',
        syncType: '',
        syncFreq: '',
        page: {
          pageNum: 1,
          pageSize: 10
        }
      },
      height: null,
      rateManageColumns,
      tableData: [],
      tableOption: {
        isShowOperate: false,
        operateWidth: '150px'
      },
      pageOption: {
        pageNum: 1,
        pageSize: 10
      },
      total: 0,
      options: [{
        value: '0',
        label: '每月'
      }, {
        value: '1',
        label: '每旬'
      }, {
        value: '2',
        label: '每周'
      }, {
        value: '3',
        label: '工作日'
      }, {
        value: '4',
        label: '每日'
      }],
      dateType: ''
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.height = this.$refs.ref2.offsetHeight
    })
    window.onresize = () => {
      this.height = this.$refs.ref2.offsetHeight
    }
    this.initData(this.searchData)
  },
  destroyed(){
    window.onresize = null
  },
  methods: {
    initData(params) {
      getrateManage(params).then(res => {
        if (res.data.code = '200') {
          this.tableData = res.data.data.list
          this.total = res.data.data.total
        }
      })
    },
    handleSelect(val) {
      console.log(val)
      this.dateType = val
    },
    inputChange(val) {
      console.log(val)
      if (this.dateType === '0') {
        if (val > 31) {
          this.$message.error('月类型日期只能为0-31')
        }
      } else if (this.dateType === '1') {
        if (val > 10) {
          this.$message.error('旬类型日期只能为0-10')
        }
      } else if (this.dateType === '2') {
        if (val > 7) {
          this.$message.error('周类型日期只能为0-7')
        }
      }
    },
    // 查询
    handleSearch() {
      this.initData(this.searchData)
    },
    // 分页
    tablePage(val) {
      this.searchData.page = val
      this.initData(this.searchData)
    },
    // 重置
    handleClear() {
      this.searchData.companyName = ''
      this.searchData.companyCode = ''
      this.searchData.syncType = ''
      this.searchData.syncFreq = ''
    },
  }
}
</script>

<style lang="less" scoped>
  .app {
    height: 100%;
    .search-bar {
      width: calc(100% - 60px);
      background-color: #ffffff;
      margin-bottom: 10px;
      padding: 10px 30px;
    }
    .table-box {
      width: 100%;
      height: calc(100vh - 210px);
      .table {
        height: 100%;
      }
    }
    .select {
      width: 100%;
    }
  }
</style>
<style>
  .table-row th {
    text-align: center;
  }
</style>
